Warning in x + y: longer object length is not a multiple of shorter object
length
[1] 100 201 302 403 504 105 206 307
Lecture 3
x and y have different lengths, and their sum is computed by recycling values of the shorter vector. [,1] [,2] [,3] [,4]
[1,] 2 3 4 5
[2,] 6 7 8 9
[3,] 10 11 12 13
[4,] 14 15 16 17
Warning in M + x: longer object length is not a multiple of shorter object
length
[,1] [,2] [,3] [,4]
[1,] 101 502 403 304
[2,] 205 106 507 408
[3,] 309 210 111 512
[4,] 413 314 215 116
* with matrices: [,1]
[1,] 5500
Chunks of code that aim to control the execution of code based on a condition.
[1] "Seven is equal to seven! Unbelievable!"
ifelse function:The if-else combination is commonly used to test conditions and handle results depending on the evaluation.
if statement, it checks only the first element and gives an error for multiple conditions.Example:
Error in if (v %% 2) : the condition has length > 1
The ifelse() function checks a condition for every element in a vector.
ifelse to choose between two vectors.v1 <- c(1,2,3,4,5,6)
v2 <- c("a","b","c","d","e","f")
ifelse(c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE), v1, v2)[1] "1" "b" "3" "d" "5" "f"
if and else if conditions.&&), OR (||), and NOT (!) are used for these conditions.[1] "Yes, x is the greatest number"
x and assign it to y:abs() function:The which() function returns the array indeces that meet a specific condition. Let us firstly contruct a dataframe
Get the row numbers where column c1 is greater than or equal to 20:
It works with matrices and arrays in general
%in% operator can be used to identify if an element (e.g., a number) belongs to a vector or dataframe.## Generates 5 values from a standard normal, 5 values from
## uniform distribution (0,1), 5 values from a normal (1,sqrt(2)):
x <- c(rnorm(n = 5), runif(n = 5), rnorm(n = 5, mean = 1,sd = 2))
f <- gl(3, 5) ## Generate levels (as.factor(rep(1:3, each=10)))
my_data <- split(x, f)
my_data$`1`
[1] -0.4727550 -1.5070332 0.4366664 -0.1690388 -1.0510129
$`2`
[1] 0.57801036 0.17650213 0.05488572 0.75356149 0.98190205
$`3`
[1] 0.7238610 3.4132464 2.0042869 -1.4766671 -0.3637311
For loops take an iterator variable and assign it successive values from a sequence or vector.seq() to generate a sequence:seq(), you can also iterate with some different step lengths:range() within seq():my_range <- range(1,10)
myvector <- seq(my_range[1], my_range[2], by = 2)
for(i in myvector){
print(i)
}[1] 1
[1] 3
[1] 5
[1] 7
[1] 9
seq_along()for body?mymarks <- c(23,29,30,30,21,25,27,30,39,19)
len_mymarks <- length(mymarks)
my_sum <- 0
for(i in (1:len_mymarks)){
my_sum <- my_sum + mymarks[i]
}
print(paste("The sum is: ", my_sum))[1] "The sum is: 273"
[1] "The mean is: 27.3"
next statement enables you to skip the current iteration of a loop without terminating it.lapply(), apply(), and tapply().lapply() functional applies a function to each element of a list, returning a listmean() function to all elements of a list. If the original list has names, then the names will be preserved in the output.[[1]]
[1] 0.8193035
[[2]]
[1] 0.9685773 0.7217290
[[3]]
[1] 0.02957967 0.05483245 0.40914464
[[4]]
[1] 0.5048155 0.1010549 0.9509554 0.2531554
runif() generates random deviates from \(U(min,max)\) (with default \(min=0\) and \(max=1\) )lapply(), it takes elements of the list and passes them as the first argument of the function you are applying.runif() is n, and so the elements of the sequence 1:4 all got passed to the n argumentlapply() may have other arguments.runif() function has min and max arguments too.lapply() comes into play.min=0 and max=10min=0 and max=10min=0 and max=10sapply() function behaves similarly to lapply(), with the primary difference being in the returned value.sapply() will try to simplify the result of lapply()sapply() calls lapply() on its input and then applies the following algorithm:
datasets library: Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
Time demand
[1,] 10 83
[2,] 20 103
[3,] 30 190
[4,] 40 160
[5,] 50 156
[6,] 70 198